<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
       <!-- 搜索结果-文章列表 -->
      <div>
        <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        >
          <article-item v-for="obj in searchResultList" :key="obj.art_id" :obj="obj"  @click.native="$router.push(`/article_detail?art_id=${obj.art_id}`)" :is-show="false"></article-item>
        </van-list>
      </div>
    </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      params: {
        page: 1,
        per_page: 10,
        q: ''// 关键词
      },
      searchResultList: [], // 搜索结果列表
      loading: false, // 控制是否加载
      finished: false
    }
  },
  created () {
    // 提取路径上参数
    this.params.q = this.$route.params.keywords
    this.getSearchResultListFn()
  },
  methods: {
    async  getSearchResultListFn () {
      // this.params.q = this.$route.params.keywords
      const res = await getSearchResultListAPI(this.params)
      this.searchResultList = [...this.searchResultList, ...res.data.data.results]
      console.log(this.searchResultList)
      this.loading = false
      if (res.data.data.results.length === 0) {
        this.finished = true // 不再让list触发加载事件
      }
    },
    // 加载更多函数
    onLoad () {
      this.params.page++
      this.getSearchResultListFn()
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
